<?php 
class Test extends CI_Controller {
	
	var $mainModel = null;
	//page details //REQUIRED
	var $formView = "test-form";
	var $listView = "test-list";
	var $FORM_FIELDS = array("firstname","middlename","lastname","username");
	var $SEARCH_FIELDS = array("firstname","middlename","lastname","username");
	var $FILE_FIELDS = array("");
	var $VALIDATION_CONFIG = "test-form";
	
	public function __construct() {
		parent::__construct();
		//load Libraries
		$this->loadLibraries();
		//load Models
		$this->loadModels();
		//load helpers
		$this->loadHelpers();
	}
	
	
	public function loadLibraries(){
		//load needed libraries here
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<font color="red">', '</font>');
	}
	
	public function loadModels(){
		//should have only one Main Model.
		$this->loadModel('test/test_model' , 'mTest' , TRUE);
	}
	
	public function loadHelpers(){
		$this->load->helper(array('form'));
	}
		
	public function loadModel( $modelFile = "", $modelAlias = "", $isMainModel = false  ){
		$this->load->model($modelFile , $modelAlias ,TRUE);
		
		if($isMainModel){
			//polymorphism 
			$this->mainModel = $this->{$modelAlias}; 
		}
	}
	
	//bind data from form
	function bindForm($isSearch = false){
		$postedValues = array_keys($_POST);
		if($isSearch){
			$formFields = array_intersect($this->SEARCH_FIELDS, $postedValues);
		}else{
			$formFields = array_intersect($this->FORM_FIELDS, $postedValues);
		}
		//retrieve data from form and assign it to array
		$data = array();
		foreach( $formFields as $field){
			if(in_array($field, $this->FILE_FIELDS)){
				$data[$field] = $_FILE[$field];
			}else{
				$data[$field] = $_POST[$field];
			}
		}
		return $data;
	}
	
	function assignEmptyValuesToForm($isSearch){
		$formFields = $isSearch ? $formFields = $this->SEARCH_FIELDS : $this->FORM_FIELDS;
		$data = array();
		foreach($formFields as $formField){
			$data[$formField] = null;
		}
		return $data;
	}
	
	function index(){
		$this->search();
	}
	
	function referenceData($data = array()){
		//other data needed in page
		return $data;
	}
	
	function add(){ //test/add
		$data = array();
		$record = array();
		if(isset($_POST['submit'])){
			$record = $this->bindForm();//method overloading
			//validate form
			if ($this->form_validation->run($this->VALIDATION_CONFIG) == FALSE){
				//do nothing
			}else{
				//insert new record
				$record = $this->preInsert($record);
				$this->mainModel->insert($record);
				$record = $this->postInsert($record);
			}
		}else{
			$record = $this->assignEmptyValuesToForm(TRUE);
		}
		$data['record'] = $record;
		$data = $this->referenceData($data);
		$this->load->view($this->formView, $data);
	}
	
	function preInsert($record = array()){
		return $record;
	}
	
	function postInsert($record = array()){
		return $record;
	}
	
	function edit($id = ""){ //test/edit/1
	    $data = array();
	    $record = array();
		if($id != ""){
			if(isset($_POST['submit'])){
				//validate fields
				$record = $this->bindForm();//method overloading
				//validate form
				if ($this->form_validation->run($this->VALIDATION_CONFIG) == FALSE){
				}else{
					$record["id"] = $id;
					$record = $this->preUpdate($record);
					$this->mainModel->update($record);
					$record = $this->postUpdate($record);
					$record = $this->mainModel->findById($id);
				}
			}else{
				$record = $this->mainModel->findById($id);
			}
		}
		
		$data['record'] = $record;
		$data = $this->referenceData($data);
		$this->load->view($this->formView, $data);
	}
	
	function preUpdate($record = array()){
		return $record;
	}
	
	function postUpdate($record = array()){
		return $record;
	}
	
	function delete($id = ""){
		if($id != ""){
			$this->mainModel->delete($id);
		}
	}
	
	function search(){
		$data = array();
	    $record = array();
	    $records = array();
		if(isset($_POST['submit'])){
			//validate fields
			$record = $this->bindForm(TRUE);//method overloading
			//validate form
			$record = $this->preSearch($record);
			$records = $this->mainModel->search($record, TRUE);
			$record = $this->postSearch($record);
		}else{
			$records = $this->mainModel->search($record);
			$record = $this->assignEmptyValuesToForm(TRUE);
		}
		$data['record'] = $record;
		$data['records'] = $records;
		
		$data = $this->referenceData($data);
		$this->load->view($this->listView, $data);
	}
	
	
	function preSearch($record = array()){
		return $record;
	}
	function postSearch($record = array()){
		return $record;
	}
}
	
?>